/**
 * 
 */
package leetCode;

/**
 * @author zhongfang
 *
 */
public class ArrayNesting {
    public static int arrayNesting(int[] nums) {
        int n=nums.length;
        int maxCount=0;
        boolean[] visited=new boolean[n];
        for (int i = 0; i < n; i++) {
			if(visited[i]){
				continue;
			}
			visited[i]=true;
			int k=i;
			int count=1;
			while (nums[k]!=i) {
				count++;
				k=nums[k];
				visited[k]=true;
			}
			if(count>maxCount){
				maxCount=count;
			}
		}
        return maxCount;
    }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] A = {5,4,0,3,1,6,2};
		System.err.println(arrayNesting(A));
	}

}
